package scala.com.shujia.jichu

object Demo15Set {
  def main(args: Array[String]): Unit = {
    //    val set1: Set[Int] = Set(11, 22, 33, 44)

    /**
     * set集合：scala中的Set集合也是不可变的，除了排序相关的函数以外，List集合有的高阶函数，Set集合也有
     */
    val set1: Set[Int] = Set(1, 4, 3, 6, 5)
    val set2: Set[Int] = Set(3, 6, 5, 7, 8)
    println(s"set1: ${set1}")
    println(s"set2: ${set2}")
    println("=" * 50)
    //求交集
    //    val resSet1: Set[Int] = set1.&(set2)
//    val resSet1: Set[Int] = set1 & set2
    val resSet1: Set[Int] = set1.intersect(set2)
    println(s"set1: ${set1}")
    println(s"set2: ${set2}")
    println(s"交集: ${resSet1}")
    println("=" * 50)
    //求并集
    //    val resSet2: Set[Int] = set1.|(set2)
    val resSet2: Set[Int] = set1 | set2
    println(s"set1: ${set1}")
    println(s"set2: ${set2}")
    println(s"并集: ${resSet2}")
    println("=" * 50)
    //求差集
    //    val resSet3: Set[Int] = set1.&~(set2)
    val resSet3: Set[Int] = set1 &~ set2
    println(s"set1: ${set1}")
    println(s"set2: ${set2}")
    println(s"差集: ${resSet3}")
    println("=" * 50)
    /**
     * Set集合和List集合能不能互相转换？
     * 可以的
     */
    val list1: List[Int] = List(11, 22, 33, 44, 55, 11, 22, 44, 88, 33, 44, 99, 11, 22, 55)
    //List->Set
    val resSet4: Set[Int] = list1.toSet
    println(s"list1:${list1}")
    println(s"resSet4:${resSet4}")
    println("=" * 50)
    //Set->List
    val list2: List[Int] = resSet4.toList.sortBy((e:Int)=>e)
    println(s"list1:${list1}")
    println(s"resSet4:${resSet4}")
    println(s"list2:${list2}")

  }
}
